package com.wdz.mvr.controllers;


import com.wdz.mvr.Services.UserService;
import com.wdz.mvr.dao.UserRepository;
import com.wdz.mvr.entities.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class SecurityController {

    @Autowired(required = true)
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    UserService userService;

    @GetMapping("/register")
    public  String register(Model model){
        User userAccount = new User();
        model.addAttribute("userAccount",userAccount);
        return "security/register";
    }

    @PostMapping("/register/save")
    public String saveUser(Model model,User userAccount){
        userAccount.setPassword(bCryptPasswordEncoder.encode(userAccount.getPassword()));
        userService.save(userAccount);
        return "redirect:/login";
    }
}
